﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Kaixin.Forms
{
    public partial class frmLogin : Form
    {
        private frmMain _parent = null;
        private bool _needCode = false;

        public frmLogin(frmMain main)
        {
            InitializeComponent();
            _parent = main;

            pnCode.Visible = false;
            this.Height = 140; // 180 for code panel


            //this.Height = 180;
            //this.pnCode.Visible = true;
            //SetCode(GetCode());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void SetCode(byte[] codedata)
        {
            if (codedata != null)
            {
                MemoryStream ms = new MemoryStream(codedata);
                pbCode.Image = new Bitmap(ms);
            }
        }

        private byte[] GetCode()
        {
            return _parent.ka.LoginGetCode();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string user = cbUser.Text.Trim();
            string pass = tbPass.Text.Trim();
            string code = tbCode.Text.Trim();
    
            /*
            if (!user.Equals("") && !pass.Equals(""))
            {
                int ret = _parent.ka.LoginWithCode(user, pass, code);
                if (ret == 1)
                    this.DialogResult = DialogResult.OK;
                else
                {
                    if (MessageBox.Show("login error, retry?", "error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question)
                        == DialogResult.Cancel)
                        this.DialogResult = DialogResult.No;
                }
            }
             * */
     

      
            if (!user.Equals("") && !pass.Equals(""))
            {
                if (_parent != null)
                {
                    int ret;

                    if (!_needCode)
                        ret = _parent.ka.Login(user, pass);
                    else
                        ret = _parent.ka.LoginWithCode(user, pass, code);

                    if (ret == 1)
                        this.DialogResult = DialogResult.OK;
                    else if (ret == -1)
                    {
                        this.Height = 180;
                        this.pnCode.Visible = true;
                        SetCode(GetCode());
                        _needCode = true;
                    }
                    else
                    {
                        if (MessageBox.Show("login error, retry?", "error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question)
                            == DialogResult.Cancel)
                            this.DialogResult = DialogResult.No;
                        else if (_needCode)
                            SetCode(GetCode());
                    }
                }
            }
       

        }

        private void pbCode_Click(object sender, EventArgs e)
        {
            SetCode(GetCode());
        }
    }
}
